package com.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shop.common.ResponseResult;
import com.shop.entity.Goods;
import com.shop.entity.Manage;
import com.shop.mapper.GoodsMapper;
import com.shop.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Transactional
@Service
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    private GoodsMapper goodsMapper;

    @Override
    public List<Goods> selectAll() {
        List<Goods> list = goodsMapper.selectList(null);
        return list;
    }

    @Override
    public ResponseResult selectPage(@RequestParam(required = false) Integer pagesize,
                                     @RequestParam(required = false) Integer pagenum,
                                     @RequestParam(required = false) String query) {
        ResponseResult result =new ResponseResult();
        Page<Goods> page = new Page<>(pagesize,pagenum);
        QueryWrapper<Goods> wrapper =new QueryWrapper<>();
        wrapper.like("goods_name",query);
        page=goodsMapper.selectPage(page,wrapper);
        result.setSuccess("查询成功",page);
        return result ;
    }
   @Override
    public ResponseResult addgoods(Goods goods) throws Exception {
        ResponseResult result = new ResponseResult<>();
       long nowTime = System.currentTimeMillis()/1000;
       Integer nowTimeInt = new Long(nowTime).intValue();
       goods.setAddTime(nowTimeInt);
       goods.setUpdTime(nowTimeInt);
       goods.setDeleteTime(nowTimeInt);
        int insert = goodsMapper.insert(goods);
        if(insert !=1){
            result.setCREATED("添加失败",goods);
            throw new Exception("添加失败");
        }
        else {
            result.setCREATED("添加成功",goods);
        }
        return result;
    }
    @Override
    public ResponseResult updateGoods(Integer id,Goods goods) throws Exception {
        ResponseResult result=new ResponseResult();
        long nowTime = System.currentTimeMillis()/1000;
        Integer nowTimeInt = new Long(nowTime).intValue();
        goods.setUpdTime(nowTimeInt);
        goods.setGoods_id(id);
        int insert = goodsMapper.updateById(goods);
        if(insert !=1){
            result.setSuccess("修改失败",goods);
            throw new Exception("修改失败");
        }
        else {
            result.setSuccess("修改成功",goods);
        }
        return result;
    }
     @Override
    public ResponseResult deleteGoods(Integer id) throws Exception {
        ResponseResult result = new ResponseResult();
        int insert=goodsMapper.deleteById(id);
        if(insert !=1){
            result.setDELETED("删除失败");
            throw new Exception("修改失败");
        }
        else {
            result.setDELETED("删除成功");
        }
        return result;
    }
    @Override
    public ResponseResult selectOne(Integer id){
        ResponseResult result=new ResponseResult();
        Goods goods=goodsMapper.selectById(id);
        if(goods==null){
            result.setSuccess("没有查询到相关内容");
        }else {
            result.setSuccess("查询成功",goods);
        }
        return result;
    }



}
